//
//  UIView+StartActivityLodingView.m
//  XClassRoom
//
//  Created by 余伟 on 2016/12/30.
//  Copyright © 2016年 Mr-yuwei. All rights reserved.
//

#import "UIView+StartActivityLodingView.h"
#include <objc/runtime.h>
static  const  void *YeeUIStartActivityLodingView= &YeeUIStartActivityLodingView;
@implementation UIView (StartActivityLodingView)
-(void)showActivitylodingViewWithstring:(NSString *)string;
{
    
    UIView  *lodingbackView=[[UIView alloc] initWithFrame:self.bounds];
    [lodingbackView setBackgroundColor:[UIColor whiteColor]];
    lodingbackView.contentMode = UIViewContentModeCenter;
    lodingbackView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin
    | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    
    [self addSubview:lodingbackView];
    UIActivityIndicatorView  *IndicatorView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    IndicatorView.center=self.center;
    IndicatorView.centerY=self.centerY-kNavBarHeight-kNavStausHeight;
    [IndicatorView startAnimating];
    [lodingbackView addSubview:IndicatorView];

    UILabel  *lable=[[UILabel alloc] init];
    lable.text=string;
    lable.font=[UIFont systemFontOfSize:13];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.textColor=[UIColor grayColor];
    lable.numberOfLines=0;
    CGFloat  height=[self sizeWithString:lable.text font:lable.font constrainedToWidth:kMainScreenWidth];
    lable.frame=CGRectMake(0, 0, self.bounds.size.width, height);
    lable.centerY=IndicatorView.bottom+10;
    [lodingbackView addSubview:lable];
    [self bringSubviewToFront:lodingbackView];
    [self setActivitylodingView:lodingbackView];
}
-(void)showActivitylodingViewWithDefaultstring
{
    [self  showActivitylodingViewWithstring:@"努力加载中..."];
}
-(void)showActivitylodingView
{
    UIView  *lodingbackView=[[UIView alloc] initWithFrame:self.bounds];
    [lodingbackView setBackgroundColor:xBackgroundColor];
    [self addSubview:lodingbackView];
    UIActivityIndicatorView  *IndicatorView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    IndicatorView.center=lodingbackView.center;
    IndicatorView.centerY=self.centerY-(kNavBarHeight+kNavStausHeight)*0.5;
    [IndicatorView startAnimating];
    [lodingbackView addSubview:IndicatorView];
    [self bringSubviewToFront:lodingbackView];
    [self setActivitylodingView:lodingbackView];
}
-(void)removeActivitylodingView
{
    if ([self ActivitylodingView])
    {
        [[self ActivitylodingView] removeFromSuperview];
    }
}
-(UIView*)ActivitylodingView
{
    return objc_getAssociatedObject(self, YeeUIStartActivityLodingView);
}
-(void)setActivitylodingView:(UIView*)lodingView
{
    objc_setAssociatedObject(self, YeeUIStartActivityLodingView, lodingView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - 计算文本
-(CGFloat)sizeWithString:(NSString *)string font:(UIFont *)font constrainedToWidth:(CGFloat)width
{
    UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
    
    CGSize textSize;
    
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
    if ([string respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
    {
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName: textFont,
                                     NSParagraphStyleAttributeName: paragraph};
        textSize = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                        options:(NSStringDrawingUsesLineFragmentOrigin |
                                                 NSStringDrawingTruncatesLastVisibleLine)
                                     attributes:attributes
                                        context:nil].size;
    } else
    {
        textSize = [string sizeWithFont:textFont
                      constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                          lineBreakMode:NSLineBreakByWordWrapping];
    }
#else
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName: textFont,
                                 NSParagraphStyleAttributeName: paragraph};
    textSize = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                    options:(NSStringDrawingUsesLineFragmentOrigin |
                                             NSStringDrawingTruncatesLastVisibleLine)
                                 attributes:attributes
                                    context:nil].size;
#endif
    
    return CGSizeMake(ceil(textSize.width), ceil(textSize.height)).height;
}

@end
